package org.grayrabbit.cms.publish.generate.factory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Category;
import org.grayrabbit.cms.publish.api.Generatable;
import org.grayrabbit.cms.publish.generate.CategoryGenerater;
import org.grayrabbit.cms.publish.generate.Generater;
import org.grayrabbit.cms.publish.generate.MultiGenerater;

import freemarker.template.Configuration;

/**
 * @author Administrator
 * @version 1.0
 * @created 15-����-2011 10:43:13
 */
public class CategoryGeneraterFactory implements GeneraterFactory {
	private Configuration configuration;
	private List<Category> categorys = new ArrayList<Category>();
	private String rootPath;
	private String[] parentPath;
	private Generatable generatable;
	private Admin admin;
	public CategoryGeneraterFactory(Configuration configuration, List<Category> categorys, String rootPath, String[] parentPath,Generatable generatable,Admin admin){
		this.configuration = configuration;
		this.categorys = categorys;
		this.rootPath = rootPath;
		this.parentPath = parentPath;
		this.generatable = generatable;
		this.admin= admin;
	}
	
	public CategoryGeneraterFactory(Configuration configuration, Category category, String rootPath,Generatable generatable,Admin admin){
		this.configuration = configuration;
		this.categorys.add(category);
		this.rootPath = rootPath;
		this.generatable = generatable;
	}


	public Generater create(){
		List<Generater> list = new ArrayList<Generater>();
		int i = 0;
		for (Category category : categorys) {
			String path = category.getCatIndexTpl().substring((File.separator+rootPath).length());
			list.add(new CategoryGenerater(configuration,path,category,parentPath[i],generatable,admin));
			i++;
		}
		if(list.size()==1)
			return list.get(0);
		return new MultiGenerater(list);
	}

}